<template>
  <div style="display: flex;">
    <div class="img" style="width: 70%;height: 100vh;background-color: black;display: flex;    justify-content: center;
    align-items: center;">
      <img src="http://47.104.167.34:9110/assets/login-logo-57e339e7.png" alt="" style="width: 300px;height: 300px;">
    </div>
    <div class="login-container">
      <!-- 登录卡片 -->
      <div class="login-card">
        <h1 class="login-title">欢迎回来</h1>
        <p class="login-title" style="color: gray;">————账号密码登录————</p>

        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" label-width="80px" class="login-form">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="loginForm.username" placeholder="请输入用户名" prefix-icon="el-icon-user"></el-input>
          </el-form-item>

          <el-form-item label="密码" prop="password">
            <el-input v-model="loginForm.password" type="password" placeholder="请输入密码"
              prefix-icon="el-icon-lock"></el-input>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="handleLogin" style="width: 100%;">
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'LoginView',
  data() {
    return {
      // 登录表单数据（与接口参数一致）
      loginForm: {
        username: '',
        password: ''
      },
      // 表单验证规则
      loginRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, message: '密码长度不能少于6位', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
async handleLogin() {
  this.$refs.loginForm.validate(async (isValid) => {
    if (!isValid) return;
    try {
      this.isLoading = true;
      const res = await this.$axios.post('/api/login', this.loginForm);
      console.log(res,res.status ==200);

      
      if (res.status ==200) {//判定成功 运行1
        console.log("已成功运行该功能");
        localStorage.setItem('adminToken', res.data.data.token);
        console.log("过第一关");
        
        this.$axios.defaults.headers.common['Authorization'] = `Bearer${res.data.data.token}`;
        console.log("过第二关");
        
        this.$router.replace('/backend');
        
        this.$message.success('登录成功！');
      }
    } catch (error) {
      console.error('登录错误详情:', error);
      this.$message.error('登录失败，请重试');
    } finally {
      console.log(this.loginForm);
      
      this.isLoading = false;
      console.log("傻了吧");
      
    }
  });
}
  }
};
</script>

<style scoped>
/* 登录页面整体样式 */
.login-container {
  width: 30%;
  height: 100vh;
  background-color: white;
  display: flex;
  justify-content: center;
  align-items: center;
}

/* 登录卡片 */
.login-card {
  width: 400px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
  padding: 30px;
}

/* 登录标题 */
.login-title {
  text-align: center;
  margin-bottom: 25px;
  color: #333;
}

/* 登录表单 */
.login-form {
  margin-top: 15px;
}
</style>